// pages/socialSystem/articalDetail/articalDetail.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        articalType: null,
        articalInfo: null,
        articalId: null,
        isCommmentInput: false,
        commentDetail: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            articalId: options.articalId,
            articalType: options.articalType
        })
        this.getArticalInfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        common.refresh()
        setTimeout(function () {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    getArticalInfo: function() {
        wx.showLoading({
            title: '加载中',
        })
        const that = this
        let data = {
            id: this.data.articalId,
            type: this.data.articalType,
            paramter: 1
        }
        common.post('Moments/moment_list', data).then(function(res) {
            wx.hideLoading()
            res[0].comment_list.reverse()
            that.setData({
                articalInfo: res[0]
            })
        }).catch(function(e) {})
    },

    commentInput: function() {
        this.setData({
            isCommmentInput: true
        })
    },

    noCommentInput: function() {
        this.setData({
            isCommmentInput: false
        })
    },

    commentChange: function(e) {
        this.setData({
            commentDetail: e.detail.value
        })
    },

    praiseClick: function() {
        const that = this
        let articalInfo = this.data.articalInfo
        if (articalInfo.is_like == 0) {
            let data = {
                moments_id: articalInfo.id
            }
            common.post('Moments/moment_like', data).then(function(res) {
                articalInfo.is_like = 1
                articalInfo.praise_num += 1
                that.setData({
                    articalInfo: articalInfo
                })
            }).catch(function(e) {})
        }
    },

    submitComment: function(e) {
        const that = this
        let data = {
            moments_id: this.data.articalInfo.id,
            content: this.data.commentDetail
        }
        common.post('Moments/moment_reply', data).then(function(res) {
            that.getArticalInfo()
            that.setData({
                commentDetail: ''
            })
        }).catch(function(e) {})
    }
})